<?php
namespace Admin\Controller;

class DomainController extends AdminBaseController
{
   public function index()
    {
        $categorys=M('DomainType')->select();
        $this->assign("categorys",$categorys);
        $this->display('domain/index');
        
    }
    
    public function cantra(){
        $categorys=M('DomainType')->select();
        $this->assign("categorys",$categorys);
        $this->display('domain/cantra');
    }
    
    public function mortdomain(){
        $categorys=M('DomainType')->select();
        $this->assign("categorys",$categorys);
        $this->display('domain/mortlist');
    }
    
    public function cantralist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $condi['a.trans_status']=array('neq',3);
        $category=I('category','');
        if($category!=''){
            $condi['a.domain_typeid']=$category;
        }
        $domain=I('domain','');
        if($domain!=''){
            $condi['a.domain']=$domain;
        }
        $uid=I('uid','');
        if($uid!=''){
            $condi['a.uid']=$uid;
        }
        $ismort=I('ismort','');
        if($ismort!=''){
            $condi['a.is_mortage']=$ismort;
        }
        $list=M('UserDomains')->join(' as a left join dd_domain_type as b on a.domain_typeid=b.id')
        ->field('a.id,a.uid,a.domain,a.domain_typeid,a.expriedate,a.regi_buines_name,a.pushid,a.is_mortage,a.trans_status,b.name')
        ->where($condi)->select();
        $count=M('UserDomains')->join(' as a left join dd_domain_type as b on a.domain_typeid=b.id')->where($condi)->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }
    public function bianlist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $uid=I('uid','');
        $category=I('category','');
        if($uid!=''){
            $condi['a.uid']=$uid;
            $counStr=' a.uid='.$uid;
        }
        if($category!=''){
            $condi['a.domain_typeid']=$category;
            $counStr=$counStr.' a.domain_typeid='.$category;
        }
        $check_status=I('check_status','');
        if($check_status!=''){
            $condi['a.check_status']=$check_status;
            $counStr=$counStr.' a.check_status='.$check_status;
        }
    
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;

        
        $UserDomain = M('UserDomains');
        $list = $UserDomain->join('as a left join dd_domain_type as b on a.domain_typeid = b.id')
        ->field('count(a.id) as num,a.checkid,a.uid,a.expriedate,MIN(a.check_status) as check_status,a.pushId,a.regi_buines_name,a.domain_typeid ,b.name')
        ->where($condi)->group('a.checkid')->order("a.createdate desc")->limit($start,$rows)->select();
       $counSql='SELECT COUNT(1) as cou FROM (SELECT a.checkid FROM dd_user_domains a WHERE 1=1 '.$counStr.' GROUP BY a.checkid) a';
       $countAr = $UserDomain->query($counSql);
        $map=array(
            'rows'=>$list,
            'total'=>$countAr[0]['cou']
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    public function dlist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
         
        $checkid=I('checkid','');
        if($checkid!=''){
            $condi['a.checkid']=$checkid;
        }
        
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
       
        
        $UserDomain = M('UserDomains');
        $list = $UserDomain->join('as a left join dd_domain_type as b on a.domain_typeid = b.id')
        ->field('a.id,a.uid,a.domain,a.price,a.check_status,a.trans_status,a.is_mortage,a.entrust,a.regi_buines_name,a.pushid,a.createdate,a.expriedate,b.name')
        ->where($condi)->order("a.createdate desc")->limit($start,$rows)->select();
        $count = $UserDomain->join('as a left join dd_domain_type as b on a.domain_typeid = b.id')->where($condi)->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }
    
    public function check(){
        $id=I('id','');
        $jieguo=I('jieguo','');
       
        if($id=='' || $jieguo==''){
            $map=array(
                'status'=>2,
                'msg'=>'域名不存在'
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
        $list = M('UserDomains')->field('id,check_status,domain_typeid,domain')->where("checkid='".$id."' and check_status=1")->select();
        if(empty($list)){
            $map=array(
                'status'=>2,
                'msg'=>'域名已经审核'
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
        $refix=M('DomainType')->field('refix')->find($list[0]['domain_typeid']);
        $sonStr=strtolower($refix['refix']);//将后缀转小写
        $domainArr=preg_split('/\s+/', $jieguo);        
        $j=0;
        $callist='';
        $k=0;
        foreach ((array)$domainArr as $jie){
            if($jie==''){
                continue;
            }
            $i=0;
            foreach($list as $domain){
                if($domain['domain']==strtolower($jie)){
                    M('UserDomains')->where('id='.$domain['id'])->setField(array('check_status'=>3,'check_result'=>'域名已PUSH,审核通过'));
                    $j=$j+1;
                    $i=1;
                }
            }
            if($i==0){
                $callist=$callist.','.$jie;
                $k=$k+1;
            }
        }
        if($k==0){
            $map=array(
                'status'=>1,
                'msg'=>'已经审核通过'.$j.'个',
            );
        }else {
            $map=array(
                'status'=>3,
                'msg'=>'审核通过'.$j.'个，其中'.$k.'个域名审核未通过',
                'calbalist'=>$callist,
            );
        }
        $this->ajaxReturn($map,'JSON');
        exit();
    }
    
    public function checkAll(){
        $ids=I('id','');
        $jieguo=I('jieguo','');
        if($ids=='' || $jieguo==''){
            $map=array(
                'status'=>2,
                'msg'=>'域名不存在'
            );
            $this->ajaxReturn($map,'JSON');
            exit();
        }
        
        $idArr = split(',', $ids);
        $j=0;
        $k=0;
        $callist="";
        $domains=strtolower($jieguo);
        $domainArr=preg_split('/\s+/', $domains);
        foreach($idArr as $id){
           
            $list = M('UserDomains')->field('id,check_status,domain_typeid,domain')->where("checkid='".$id."' and check_status=1")->select();
            if(empty($list)){
                continue;
            }
           foreach ($domainArr as $jie){
               if($jie==''){
                   continue;
               }
               $i=0;
                foreach($list as $domain){
                    if($domain['domain']==$jie){
                        M('UserDomains')->where('id='.$domain['id'])->setField(array('check_status'=>3,'check_result'=>'域名已PUSH,审核通过'));
                        $j=$j+1;
                        $i=1;
                    }
                }
                if($i==0){
                    $callist=$callist.','.$jie;
                    $k=$k+1;
                }
            }
        }
        if($k==0){
            $map=array(
                'status'=>1,
                'msg'=>'已经审核通过'.$j.'个',
            );
        }else {
            $map=array(
                'status'=>3,
                'msg'=>'审核通过'.$j.'个，其中'.$k.'个域名审核未通过',
                'calbalist'=>$callist,
            );
        }
        $this->ajaxReturn($map,'JSON');
        exit();
    }
    
    public function mortdomlist(){
        if(session('auid')==null){
            $this->display('index/login');
            exit();
        }
        $uid=I('uid','');
        if($uid!=''){
            $condi['a.uid']=$uid;
        }
        $domain=I('domain','');
        if($domain!=''){
            $condi['a.domain']=$domain;
        }
        $condi['a.is_mortage']=2;
        $page=I('page',1);
        $rows=I('rows',50);
        $start=($page-1)*$rows;
    
        $UserDomain = M('UserDomains');
        $list = $UserDomain->join('as a left join dd_credflow_domian as b on a.id = b.domainid')
        ->field('a.id,a.pushid,a.domain,a.regi_buines_name,a.uid,a.expriedate,b.credit')
        ->where($condi)->order("a.createdate desc")->limit($start,$rows)->select();
        $count = $UserDomain->join('as a left join dd_credflow_domian as b on a.id = b.domainid')->where($condi)->count();
        $map=array(
            'rows'=>$list,
            'total'=>$count
        );
        $this->ajaxReturn($map,'JSON');
    }
    public function dwd(){
        $callist=array();
        $domains='1,2,3,4,5';
        $sonStr=',';
        $domainArr = explode($sonStr,$domains);
        foreach ($domainArr as $ed){
            $ed=$ed.'10';
            $mapd=array('key'=>$ed);
            array_push($callist, $mapd);
        }
        echo '------------------'.json_encode($callist);
    }
}